package com.zkx.fssb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zkx.fssb.domain.po.Cake;
import com.zkx.fssb.mapper.CakeMapper;
import com.zkx.fssb.result.PageQuery;
import com.zkx.fssb.service.CakeService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class CakeServiceImpl extends ServiceImpl<CakeMapper, Cake> implements CakeService {


    @Override
    public Page customPage(PageQuery pageQuery) {

        Page page = new Page(pageQuery.getPageNo(), pageQuery.getPageSize());
        LambdaQueryWrapper<Cake> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(pageQuery.getCakeName() != null, Cake::getCakeName, pageQuery.getCakeName())
                .eq(pageQuery.getCakeStatus() != null, Cake::getCakeStatus, pageQuery.getCakeStatus())
                .orderByDesc(Cake::getCakeStatus);
        this.page(page, wrapper);
        List<Cake> records = page.getRecords();
        records = records.stream().map(item -> {
            if (item.getCakeStock() == 0) {
                item.setCakeStatus(0);
            }
            return item;
        }).collect(Collectors.toList());

        return page;



    }
}
